﻿using MotionIoLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using csEIO;
using System.Windows.Forms;

namespace MotionIoLib
{
    public  class IoCtrl_EIO : IoCtrl
    {

        #region 属性  
        //卡的数量
        volatile UInt16 _totalcards = 1;
        public UInt16 TotalCards
        {
            get
            {
                return _totalcards;
            }
            set
            {
                _totalcards = value;
            }
        }

        volatile UInt16 _slavecards = 1;
        public UInt16 SlaveCards
        {
            get
            {
                return _slavecards;
            }
            set
            {
                _slavecards = value;
            }
        }

        //IO卡卡号数组
        volatile UInt16[] _cardnamearray = new UInt16[8] { 0, 1, 2, 3, 4, 5, 6, 7 };
        UInt16[] CardNameArray
        {
            get
            {
                return _cardnamearray;
            }
            set
            {
                _cardnamearray = value;
            }
        }

        //网段数组，默认为167
        UInt16[] _section = new UInt16[8] { 167, 167, 167, 167, 167, 167, 167, 167 };
        public UInt16[] Section
        {
            get
            {
                return _section;
            }
            set
            {
                _section = value;
            }
        }

        //主机地址数组
        volatile UInt16[] _hostid = new UInt16[8] { 120, 121, 122, 123, 124, 125, 126, 127 };
        public UInt16[] HostID
        {
            get
            {
                return _hostid;
            }
            set
            {
                _hostid = value;
            }
        }
        #endregion
        public IoCtrl_EIO(int nIndex, ulong nCardNo) : base(nIndex, nCardNo)//Anicet
        {
            m_strCardName = "IoCtrl_EIO";
        }

        public override void DeInit()
        {
            if(EIO.Plt_CardClose() == 0)//关闭IO卡
            {
                m_bOpen = false;
                return;
            }
               
        }

        public override bool Init()
        {
            EIO.Plt_CardClose();
            m_bOpen = false;
            short n = EIO.Plt_CardOpen(_totalcards, _cardnamearray, _section, _hostid);

            if (n == 0)
            {
                m_bOpen = true;
                return true;
            }
            logger.Warn(string.Format("{0} 号卡{1} 打开失败 请检查配置", m_nCardNo, m_strCardName));
            MessageBox.Show(string.Format("{0} 号卡{1} 打开失败 请检查配置", m_nCardNo, m_strCardName), "Err", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        public override bool InStopDisenable(int nIndex)
        {
            return true;
        }

        public override bool InStopEnable(int nIndex)
        {
            return true;
        }

        ///获取卡所有的输入信号 
        public override bool ReadIOIn(ref int nData)//其它卡都是直接返回true
        {
            UInt32 Data = 0;
            if (!m_bOpen)
            {
                Data = 0xFFFFFFFF;
                nData = (int)Data;
                return false;
            }
            Data = (UInt32)nData;

            if (EIO.Plt_IoReadAllInput((UInt16)m_nCardNo, ref Data) == 0)
                return true;

          
            return false;
        }

        /// <summary>
        ///按位获取输入信号 
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public override bool ReadIoInBit(int nIndex)
        {
            UInt16 active_level = 0;
            if (!m_bOpen)
            {
                active_level = 1;
                return false;
            }
            EIO.Plt_IoReadInputByBit((UInt16)m_nCardNo, (UInt16)nIndex, ref active_level);
            return (active_level == 0);
        }


        /// <summary>
        ///获取卡所有的输出信号 
        /// </summary>
        /// <param name="nData"></param>
        /// <returns></returns>
        public override bool ReadIOOut(ref int nData)
        {
            UInt32 Data = 0;
            if (!m_bOpen)
            {
                Data = 0xFFFFFFFF;
                nData = (int)Data;
                return false;
            }
            Data = (UInt32)nData;


            if (EIO.Plt_IoReadAllOutput((UInt16)m_nCardNo, ref Data) == 0)
                return true;

           
            return false;
        }

        /// <summary>
        ///按位获取输出信号 
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public override bool ReadIoOutBit(int nIndex)
        {
            UInt16 active_level = 0;
            if (!m_bOpen)
            {
                active_level = 1;
                return false;
            }
            EIO.Plt_IoReadOutputByBit((UInt16)m_nCardNo, (UInt16)nIndex, ref active_level);
            return (active_level == 0);
        }


        /// <summary>
        /// 输出整个卡的信号 
        /// </summary>
        /// <param name="nData"></param>
        /// <returns></returns>
        public override bool WriteIo(int nData)
        {
            //Plt_IoWriteAllOutput
            UInt32 Data = 0;
            if (!m_bOpen)
            {
           
                return false;
            }
            Data = (UInt32)nData;

            if (EIO.Plt_IoWriteAllOutput((UInt16)m_nCardNo, Data) == 0)
                return true;
            return false;
        }


        /// <summary>
        /// 按位设置输出信号 
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="bBit"></param>
        /// <returns></returns>
        public override bool WriteIoBit(int nIndex, bool bBit)
        {
            if (!m_bOpen)
                return false;


            return (EIO.Plt_IoWriteOutputByBit((UInt16)m_nCardNo, (UInt16)nIndex, bBit ? ((UInt16)0) : ((UInt16)1)) == 1);
            
        }
    }
}
